package com.wbdgj.ui.booking;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.ChoiceRoomAdapter;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.NetUtils;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;
import com.wbdgj.views.RichTextView;
import com.wbdgj.views.update.CommonProgressDialog;
import com.yanzhenjie.alertdialog.AlertDialog;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class ChoiceRoomDialogActivity extends Activity{

    private Context context = this;
    private String CHECKIN_DATE;
    private String CHECKOUT_DATE;
    private String id;
    private String IS_HOUR;

    private GridView roomGrid;
    private ChoiceRoomAdapter choiceRoomAdapter;
    private List<LinkedTreeMap> roomList;
    private TextView xzlTxt;

    private String roomId;
    private String roomIdNum;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_choice_room_dialog);
        getWindow().getAttributes().width = RelativeLayout.LayoutParams.MATCH_PARENT;
        getWindow().getAttributes().gravity = Gravity.BOTTOM;

        roomGrid = findViewById(R.id.roomGrid);
        xzlTxt = findViewById(R.id.xzlTxt);

        CHECKIN_DATE = getIntent().getStringExtra("CHECKIN_DATE");
        CHECKOUT_DATE = getIntent().getStringExtra("CHECKOUT_DATE");
        id = getIntent().getStringExtra("ROOM_TYPE_ID");
        IS_HOUR = getIntent().getStringExtra("IS_HOUR");
        roomId = getIntent().getStringExtra("roomID");
        roomIdNum = getIntent().getStringExtra("roomIdNum");
        xzlTxt.setText(roomIdNum);
        room_info();

        findViewById(R.id.closeImg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.ljyd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(roomId)){
                    ToastUtils.toastShort("请选择房间！");
                }else{
                    Intent i = new Intent();
                    i.putExtra("roomId", roomId);
                    i.putExtra("roomIdNum", roomIdNum);
                    setResult(2, i);
                    finish();
                }
            }
        });

        roomGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                if(decimalFormat.format(roomList.get(i).get("room_status")).equals("0")){
                    Log.e("zp", "id="+decimalFormat.format(roomList.get(i).get("id")));
                    choiceRoomAdapter = new ChoiceRoomAdapter(context, roomList, decimalFormat.format(roomList.get(i).get("id")));
                    roomGrid.setAdapter(choiceRoomAdapter);
                    roomId = decimalFormat.format(roomList.get(i).get("id"));
                    roomIdNum = roomList.get(i).get("room_number")+"";
                    xzlTxt.setText(roomIdNum);
                }else{
                    ToastUtils.toastShort("当前房间已入住！");
                }

            }
        });
    }

    private void room_info() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().room_info(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), CHECKIN_DATE, CHECKOUT_DATE, id,"0");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    roomList = new ArrayList<>();
                    roomList = (List<LinkedTreeMap>) userModelBaseResultModel.getData();
                    choiceRoomAdapter = new ChoiceRoomAdapter(context, roomList, roomId);
                    roomGrid.setAdapter(choiceRoomAdapter);

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
